package cn.congine.wizarpos.mall.action;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import cn.congine.wizarpos.mall.model.ErpProduct;
import cn.congine.wizarpos.mall.model.open.Category;
import cn.congine.wizarpos.mall.model.open.Merchandise;
import cn.congine.wizarpos.mall.model.open.MerchandiseSyncInput;
import cn.congine.wizarpos.mall.model.open.MerchandiseSyncOutput;
import cn.congine.wizarpos.mall.model.open.ProductCategorySyncInput;
import cn.congine.wizarpos.mall.model.open.ProductCategorySyncOutput;
import cn.congine.wizarpos.mall.service.impl.WizarposOpenRestClient;
import cn.congine.wizarpos.mall.utils.Function;
import cn.congine.wizarpos.mall.utils.SysConstants;

@Controller
@RequestMapping(value = "/erp_product_category")
public class ErpProductCategoryAction {
	
	private static Logger logger = Logger.getLogger(ErpProductCategoryAction.class);
	
	@Autowired
	private WizarposOpenRestClient wizarposOpenRestClient;
	
	/**
	 * 获得所有分类
	 * 
	 * @param openId
	 * @param mid
	 * @return
	 */
	@RequestMapping(value = "/categoryList", method = RequestMethod.GET)
	public ModelAndView categoryList(
			@RequestParam(value = "mid") String mid,
			@RequestParam(value = "categoryId", required = false) String categoryId,
			HttpServletRequest httpRequest) {
		
		if(StringUtils.isEmpty(categoryId)) {
			categoryId = "0"; // 0为根目录
		}
		
		ProductCategorySyncInput input = new ProductCategorySyncInput();
		input.setMid(mid);
		input.setParentId(categoryId);
		
		ProductCategorySyncOutput output = (ProductCategorySyncOutput) wizarposOpenRestClient.post(input,"/product/category",ProductCategorySyncOutput.class);
		List<Category> categoryList = output.getResult();
		
		for (Category e : categoryList) {
			input.setParentId(e.getId());
			ProductCategorySyncOutput childOutput = (ProductCategorySyncOutput) wizarposOpenRestClient.post(input,"/product/category",ProductCategorySyncOutput.class);
			List<Category>  childList = childOutput.getResult();
			
			if (childList != null && childList.size() > 0) {// 有子类分类
				e.setHasChild(true);
			} else {
				e.setHasChild(false);
			}
		}
		
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("first", false);
		data.put("categoryList", categoryList);
		data.put("mid", mid);
		
		return new ModelAndView("productcategory/secondclass", data);
	}
	
	@RequestMapping(value = "/categoryIndex", method = RequestMethod.GET)
	public ModelAndView categoryIndex(
			@RequestParam(value = "mid") String mid,
			@RequestParam(value = "categoryId", required = false) String categoryId,
			@RequestParam(value = "hotJudge", required = false) String hotJudge,
			@RequestParam(value = "keyWord", required = false) String keyWord,
			HttpServletRequest httpRequest)
			throws UnsupportedEncodingException {
		MerchandiseSyncInput input = new MerchandiseSyncInput();
		input.setMid(mid);
		input.setCategoryId(categoryId);
		input.setHot(hotJudge);
		MerchandiseSyncOutput output = (MerchandiseSyncOutput) wizarposOpenRestClient.post(input,"/product/merchandise",MerchandiseSyncOutput.class);
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> getpricehot = new HashMap<String, Object>();
		Map<String, Object> getmberpricehot = new HashMap<String, Object>();
		List<Merchandise> list = output.getResult();
		List<ErpProduct> producthot = new ArrayList<ErpProduct>();

		for (Merchandise merchandise : list) {
			getpricehot.put(merchandise.getProductId(), merchandise.getPrice());
			getmberpricehot.put(merchandise.getProductId(), merchandise.getMemberPrice());
			
			ErpProduct erpProduct = new ErpProduct();
			erpProduct.setShowMemPrice(merchandise.getMemberPrice());
			erpProduct.setId(merchandise.getProductId());
			erpProduct.setName(merchandise.getName());
			
			// 商品图片---多张			
			if (StringUtils.isEmpty(merchandise.getPicUrl())) {
				erpProduct.setPicUrl(SysConstants.DEFAULT_PICTURE);
			} else {
				erpProduct.setPicUrl(Function.dealGridPicUrl(merchandise.getPicUrl(), 300, 300));
			}

			formatProdName(erpProduct);
			producthot.add(erpProduct);
		}

		data.put("getpricehot", getpricehot);
		data.put("getmberpricehot", getmberpricehot);
		data.put("producthot", producthot);
		data.put("categoryId", categoryId);
		data.put("mid", mid);
		return new ModelAndView("productcategory/prolist", data);
	}
	
	/**
	 * 商品名称过长，做部分截取
	 * 
	 * @param erpProduct
	 */
	private void formatProdName(ErpProduct erpProduct) {
		String name = erpProduct.getName();
		if (name != null && name.length() > 23) {
			erpProduct.setName(name.substring(0, 20) + "...");
		}
	}
}
